package config

import "github.com/go-playground/validator/v10"

// Config 代表了server的配置
type Config struct {
	LogLevel string `mapstructure:"log_level" validate:"required,oneof=debug info"`

	HTTP     HTTPConfig     `mapstructure:"http" validate:"required"`
	MySQL    MySQLConfig    `mapstructure:"mysql" validate:"required"`
	Security SecurityConfig `mapstructure:"security" validate:"required"`
	OSS      OssConfig      `mapstructure:"oss" validate:"required"`
}

type HTTPConfig struct {
	APP_URL         string `mapstructure:"app_url" validate:"required,url"`
	Address         string `mapstructure:"address" validate:"required,tcp_addr"`
	SessionDuration int    `mapstructure:"session_duration" validate:"required,numeric"`
	URLPrefix       string `mapstructure:"url_prefix" validate:"required,startswith=/,endswith=/"`
}

type SecurityConfig struct {
	JWTSecret    string `mapstructure:"jwt_secret" validate:"required"`
	PasswordSalt string `mapstructure:"password_salt" validate:"required"`
}

type OssConfig struct {
	AccessKeyID     string `mapstructure:"access_key_id" validate:"required"`
	AccessKeySecret string `mapstructure:"access_key_secret" validate:"required"`
	EndPoint        string `mapstructure:"endpoint" validate:"required"`
	BucketName      string `mapstructure:"bucket_name" validate:"required"`
}

type MySQLConfig struct {
	DSN string `mapstructure:"dsn" validate:"required"`
}

// Server 是当前运行server的配置，只读访问
// 【警告】禁止对该变量进行任何写操作！
var Server = Config{}

// Validate 检查配置是否有效
func (cfg *Config) Validate() error {
	return validator.New().Struct(&Server)
}
